<?php

/*
 * ************************************************* 
 * Created on :2012-3-19 23:35:56
 * Encoding   :UTF-8
 * Description:
 * 
 * @Author @lqs <soso2501@gmail.com>
 * @WAP_WEIBO (C)1996-2099 SINA Inc. 
 * ************************************************
 */

class Router {

    var $actionName = 'index';
    var $controlName = 'main';
    var $controller;
    var $controllerpath;

    public function __construct() {
        $this->dealURL();
    }
    public function getcontrollerpath(){
        if(empty($this->controllerpath))
            $this->controllerpath = ROOT_DIR.'controller/'.$this->controlName.".php";
        return $this->controllerpath;
    }

    public function dealURL() {
        if(empty($_SERVER['REDIRECT_URL'])) return;
        $r = trim($_SERVER['REDIRECT_URL'], '/');
        if (empty($r))
            return;
        if (strpos($r, '/') === false)
            $this->controlName = $r;
        else {
            list($this->controlName, $this->actionName) = explode('/',$r);
        }
    }
    public function dispatch(){
        if(file_exists($this->getcontrollerpath()))
            require_once($this->getcontrollerpath ());
        else
            throw new LException("找不到相应的控制器({$this->controlName})");
        $this->controller = new $this->controlName();
        $this->controller->{$this->actionName}();
    }
}

/* End of file router */


